<?php
/**
 * Created by IntelliJ IDEA. 
 * User: cmn
 * Date: 2020-10-16
 * Time: 20:58
 */

namespace tech\esign_core;


use tech\esign_utils\CommonHelper;
use tech\esign_utils\HttpHelper;

class TemplateHelper extends BaseHelper
{
    //填充指定PDF模板文件，指定PDF模板文档路径及待填充文本域的Key-Value，完成PDF模板文件的填充。
    public function createFromTemplateFile($srcFileUrl,$dstFileUrl,$txtFields,$flatten=true,$ownerPassword=null){
        $data = [
            'srcFileUrl'=>$srcFileUrl,
            'dstFileUrl'=>$dstFileUrl,
            'ownerPassword'=>$ownerPassword,
            'txtFields'=>$txtFields,
            'flatten'=>$flatten,
        ];
        $result = HttpHelper::doPost($this->warUrl.$this->urlMap['CREATE_FROM_TEMPLATE_FILE'],$data);
        return json_decode($result,true);
    }


    //填充指定PDF模板流
    public function createFromTemplateStream($srcPath,$txtFields,$flatten=true,$ownerPassword=null){
        $srcPdfFile = HttpHelper::encodePath($srcPath);
        if (! CommonHelper::isUrl($srcPdfFile)) {
            return array(
                'errCode' => 913,
                'msg' => 'srcPdfFile 路径不是远程Url',
            );
        }
        $file = CommonHelper::getRealFileIgnore($srcPdfFile);
        $data = [
            "file"=>$file,
            "ownerPassword"=>$ownerPassword,
            'txtFields'=>json_encode($txtFields),
            'flatten'=>$flatten,

        ];
        $result =  HttpHelper::postFile($this->warUrl.$this->urlMap['CREATE_FROM_TEMPLATE_STREAM'],$data);
        return json_decode($result,true);

    }

}
